import 'dart:async';

import 'package:ayj_flutter_chat_app/database/friend_list/friend_info_db.dart';
import 'package:ayj_flutter_chat_app/database/friend_list/friend_list_dao.dart';
import 'package:ayj_flutter_chat_app/database/group_list/group_info_db.dart';
import 'package:floor/floor.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart' as sqflite;

import '../models/file_manager.dart';
import '../pages/label/label_add/model/friend_info_entity.dart';
import '../service/log_utils.dart';
import 'group_list/group_list_dao.dart';
import 'message/message_dao.dart';
import 'message/message_db.dart';

part 'app_database.g.dart';

@Database(version: 1, entities: [FriendInfoDb, MessageDb, GroupInfoDb])
abstract class AppDatabase extends FloorDatabase {
  MessageDao get messageDao;

  FriendListDao get friendListDao;

  GroupListDao get groupListDao;

  static const String _databaseName = 'app_database.db';

  static Future<AppDatabase> init() async {
    String basePath = await FileManager.getFilePath(type: FileManagerType.database);
    String path = join(basePath, _databaseName);
    LogUtils.d('Database path: $path');
    FileManager.createFile(_databaseName, type: FileManagerType.database);

    return await $FloorAppDatabase.databaseBuilder(path).addMigrations([]).build();
  }

  static Future<void> delete() async {
    String basePath = await FileManager.getFilePath(type: FileManagerType.database);
    String path = join(basePath, _databaseName);
    await sqflite.deleteDatabase(path);
  }

  static Future<bool> databaseExists(String path) {
    return sqflite.databaseExists(path);
  }
}
